package com.ruoyi.product.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.dto.admin.product.ExamineOrderDTO;
import com.ruoyi.business.domain.dto.admin.product.OrderDTO;
import com.ruoyi.business.domain.product.TbOrder;
import com.ruoyi.business.domain.vo.order.OrderVo;
import com.ruoyi.business.domain.vo.product.OrderExamineVo;
import com.ruoyi.business.service.product.TbOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 订单Controller
 */
@RestController
@RequestMapping("/business/product/order")
@RequiredArgsConstructor
@Api(tags = "订单管理接口")
public class TbOrderController extends BaseController {

    private final TbOrderService tbOrderService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    @SaCheckPermission("business:product:order:query:list")
    @ApiOperation("查询订单列表")
    public TableDataInfo<OrderVo> list(OrderDTO tbOrderDTO) {
        return tbOrderService.pointsMallOrder(tbOrderDTO);
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取订单详细信息")
    @SaCheckPermission("business:product:order:query:id")
    public R<OrderVo> getInfo(@PathVariable("id") Long id) {
        return R.ok(tbOrderService.getDetilById(id));
    }

    /**
     * 新增订单
     */
    @PostMapping
    @SaCheckPermission("business:product:order:add")
    public R add(@RequestBody TbOrder tbOrder) {
        return toAjax(tbOrderService.save(tbOrder));
    }

    /**
     * 修改订单
     */
    @PutMapping
    @ApiOperation("修改订单")
    @SaCheckPermission("business:product:order:edit")
    public R edit(@RequestBody TbOrder tbOrder) {
        return toAjax(tbOrderService.updateById(tbOrder));
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{ids}")
    @SaCheckPermission("business:product:order:remove:ids")
    public R remove(@PathVariable Long[] ids) {
        TbOrder tbOrder = new TbOrder();
        tbOrder.setIsDelete(1);
        boolean update = tbOrderService.update(tbOrder, new LambdaQueryWrapper<>(TbOrder.class).in(TbOrder::getId, Arrays.asList(ids)));
        return toAjax(update);
    }

    @PostMapping("/orderExamine")
    @ApiOperation("订单审核")
    @SaCheckPermission("business:product:order:orderExamine")
    public R<List<OrderExamineVo>> orderExamine(@RequestBody ExamineOrderDTO examineOrderDTO) {
        return tbOrderService.orderExamine(examineOrderDTO);
    }
    @PostMapping("/orderCancel")
    @ApiOperation("吉客云订单取消")
    @SaCheckPermission("business:product:order:orderCancel")
    public R<Boolean> orderCancel(@RequestBody ExamineOrderDTO examineOrderDTO) {
        return tbOrderService.orderCancel(examineOrderDTO);
    }
}
